﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.Sys.Orders.GetMoney
{
    public partial class Form : FormPage
    {
        BLL.Orders bll = new BLL.Orders();
        BLL.V_Orders bllV_Orders = new BLL.V_Orders();
        List<Model.V_Orders> listModelV_Orders = new List<Model.V_Orders>();
        //
        BLL.Orders_Goods bllOrders_Goods = new BLL.Orders_Goods();
        BLL.Goods bllGoods = new BLL.Goods();
        BLL.Supplier bllSupplier = new BLL.Supplier();
        BLL.Store bllStore = new BLL.Store();
        //
        decimal ExpectPay_ALL = 0;
        protected void Page_Load(object sender, EventArgs e) {  }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() {  }
        public override void Page_Load_More()
        {
            if (Master.idStr != "")
            {
                listModelV_Orders = bllV_Orders.GetModelList("id in(" + Master.idStr + ")");
                foreach (Model.V_Orders modelV_Orders in listModelV_Orders) ExpectPay_ALL += BLL.Orders.Get_Orders_ExpectPay(modelV_Orders.id) + 100;

                rp_Orders.DataSource = listModelV_Orders;
                rp_Orders.DataBind();
                //
                ((Label)rp_Orders.Controls[rp_Orders.Controls.Count - 1].FindControl("lb_ExpectPay_ALL")).Text = StringPlus.showMoney(ExpectPay_ALL);
            }
        }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            foreach (string id in Master.idStr.Split(','))
            {
                int thisID = DataTypePlus.requestInt(id);
                //string ExpectBuyPriceSum = StringPlus.showMoney(BBPA.BLL.Orders.Get_Orders_ExpectBuyPriceSum(thisID));
                //订单
                Model.Orders model = bll.GetModel(thisID);
                model.ExpectBuyPriceSum = 0;//预计联盟采购总价
                model.ExpectPay = 0;//预计支付采购款
                foreach (Model.Orders_Goods modelOrders_Goods in bllOrders_Goods.GetModelList("OrdersID=" + thisID))//货品：预计联盟价格+预计小计
                {
                    Model.Goods modelGoods = bllGoods.GetModel(modelOrders_Goods.GoodsID);
                    //预计活动
                    modelOrders_Goods.ExpectBuyDiscount = BLL.Goods.GetBuyDiscount(modelOrders_Goods.GoodsID, modelOrders_Goods.ExpectAmount);
                    modelOrders_Goods.ExpectBuyGiftNumber = BLL.Goods.GetGiftNumberBuy(modelOrders_Goods.GoodsID, modelOrders_Goods.ExpectAmount);
                    //预计采购价格
                    modelOrders_Goods.ExpectBuyPrice = modelGoods.BuyPrice;
                    modelOrders_Goods.ExpectBuyPriceSum = BLL.Goods.GetBuyPriceSum(modelOrders_Goods.GoodsID, modelOrders_Goods.ExpectAmount);//参加活动之后的小计
                    model.ExpectBuyPriceSum += modelOrders_Goods.ExpectBuyPriceSum;
                    //预计支付金额
                    Model.Supplier modelSupplier = bllSupplier.GetModel(modelGoods.SupplierID);
                    if (modelSupplier != null && !modelSupplier.IsLatePay)
                    {
                        modelOrders_Goods.ExpectPay = modelOrders_Goods.ExpectBuyPriceSum;
                        //收货和现场分开
                        if (modelSupplier.IsDelivery) model.ExpectPay_Delivery += modelOrders_Goods.ExpectPay;
                        else model.ExpectPay += modelOrders_Goods.ExpectPay;
                    }
                    //
                    bllOrders_Goods.Update(modelOrders_Goods);
                }

                model.State = (int)EnumHelper.OrdersState.Buying;//修改订单状态:Buying
                bll.Update(model);
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Orders, thisID, EnumHelper.OperateType.Update, "打采购款" + model.ExpectPay + "，预计联盟采购价格，修改订单状态:开始采购");
                //订单状态日志
                BLL.Orders_State_Log bllOrders_State_Log=new BLL.Orders_State_Log();
                Model.Orders_State_Log modelOrders_State_Log = new Model.Orders_State_Log();
                modelOrders_State_Log.OrdersID = thisID;
                modelOrders_State_Log.State = model.State;
                modelOrders_State_Log.Description = "打采购款" + model.ExpectPay;
                int Orders_State_LogID_new = bllOrders_State_Log.Add(modelOrders_State_Log);
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Orders_State_Log, Orders_State_LogID_new, EnumHelper.OperateType.Add, "打采购款" + model.ExpectPay);
                //Stat
                //Orders
                BLL.Stat.Increase1(EnumHelper.StatTable.Orders_Stat, EnumHelper.Orders_Stat.Count, model.id);
                //Store
                BLL.Stat.Increase1(EnumHelper.StatTable.Store_Stat, EnumHelper.Store_Stat.OrdersCount,model.StoreID);
            }
            Master.SubmitOK("预支采购款成功，订单开始采购");
        }
        
    }
}